package boss_service

import (
	"beta/models"
	"beta/pkg/e"
	"beta/pkg/logger"
	"beta/pkg/util"
	"errors"
	"time"
)

type BossService struct {
	Operator string
	Hosts map[string]*models.AppEnv
	TraceId string
}

var (
	appName = "boss"
)

func (b BossService) Login() (token string, err error) {
	loginUser := e.GetOperatorMap(b.Operator)

	host := b.Hosts[appName].AppDomain
	logger.ZapLogger.Infow("【boss登录】", "请求参数", loginUser, "traceId", b.TraceId)

	httpClient := util.NewHttpClient(3 * time.Second).R()

	resp, respErr := httpClient.SetFormData(loginUser).Post(host + "/xqy-portal-web/boss/login/base/login")
	if respErr != nil {
		return "", respErr
	}
	logger.ZapLogger.Infow("【boss登录】", "请求结果", resp, "traceId", b.TraceId)

	token = util.GetCookieValueByName(resp.Cookies(), util.BossCookieName)
	if token == "" {
		return "",errors.New(util.BossCookieName + "不存在")
	}

	return token, nil
}